/*************************************************************************
 *  Compilation:  javac BlackScholes.java MyMath.java
 *  Execution:    java BlackScholes S X r sigma T
 *  
 *  Reads in five command line inputs and calculates the option price
 *  according to the Black-Scholes formula.
 *
 *  % java BlackScholes 23.75 15.00 0.01 0.35 0.5
 *  8.879159279691955                                  (actual =  9.10)
 *      
 *  % java BlackScholes 30.14 15.0 0.01 0.332 0.25
 *  15.177462481562186                                 (actual = 14.50)
 *
 *
 *  Information calculated based on closing data on Monday, June 9th 2003.
 *
 *      Microsoft:   share price:             23.75
 *                   strike price:            15.00
 *                   risk-free interest rate:  1%
 *                   volatility:              35%          (historical estimate)
 *                   time until expiration:    0.5 years
 *
 *       GE:         share price:             30.14
 *                   strike price:            15.00
 *                   risk-free interest rate   1%
 *                   volatility:              33.2%         (historical estimate)
 *                   time until expiration     0.25 years
 *
 *
 *  Reference:  http://www.hoadley.net/options/develtoolsvolcalc.htm
 *  Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
 *  Last updated: Wed Feb 9 09:05:37 EST 2011. 
 *************************************************************************/

package LAAMI;

import repast.simphony.engine.environment.RunEnvironment;

public class BlackScholes {
	
	public static double getPrice(double sigma) {
		double S = LAAMIBuilder.getStockmarket().getPrice();
		double X = LAAMIBuilder.getOptionmarket().getStrikePrice();
		double r = LAAMIBuilder.getInterestRate();
		double T = calculateTime();
        double d1 = (Math.log(S/X) + (r + sigma * sigma/2) * T) / (sigma * Math.sqrt(T));
        double d2 = d1 - sigma * Math.sqrt(T);
        return S*Gaussian.Phi(d1) - X * Math.exp(-r * T) * Gaussian.Phi(d2);
    }
	
	// Return remaining time to option expiration (in years)
	public static double calculateTime() {
		double maturity = LAAMIBuilder.getMaturity();
		double day = RunEnvironment.getInstance().getCurrentSchedule()
				.getTickCount();
		return (maturity - (day - 50) / 252);
	}
}
